//
//  MenuHeaderView.m
//  LiquorChain
//
//  Created by apple on 2018/12/25.
//  Copyright © 2018年 王冬. All rights reserved.
//

#import "MenuHeaderView.h"

@interface MenuHeaderViewCell : UICollectionViewCell

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *lineView;
- (void)showLine;
- (void)hideLine;

@end

@implementation MenuHeaderViewCell
{
    BOOL _show;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _show = NO;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = [UIColor darkGrayColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        self.lineView = [[UIView alloc] initWithFrame:CGRectZero];
        _lineView.backgroundColor = [UIColor redColor];
        [self addSubview:_lineView];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    if (_show)
    {
        self.lineView.hidden = NO;
        self.lineView.frame = CGRectMake(0, self.frame.size.height - 2, self.titleLabel.frame.size.width/2, 2);
        self.lineView.center = CGPointMake(self.titleLabel.center.x, self.lineView.center.y);
    }
    else
    {
        self.lineView.hidden = YES;
    }
}

- (void)showLine
{
    _show = YES;
    [self setNeedsLayout];
}

- (void)hideLine
{
    _show = NO;
    [self setNeedsLayout];
}

@end



@interface MenuHeaderView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIView *lineView;

@end
@implementation MenuHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 1, frame.size.width, 1)];
        bottomLine.backgroundColor = [UIColor darkGrayColor];
        [self addSubview:bottomLine];
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[MenuHeaderViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MenuHeaderViewCell class])];
        
        [self addSubview:self.collectionView];
        
        _currentIndex = 0;
        
        self.lineView = [[UIView alloc] initWithFrame:CGRectZero];
        self.lineView.backgroundColor = [UIColor redColor];
        [self addSubview:self.lineView];
    }
    return self;
}

- (void)setCurrentIndex:(NSInteger)currentIndex
{
    _currentIndex = currentIndex;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:currentIndex inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionCenteredHorizontally) animated:YES];
    [self.collectionView reloadData];
}

- (void)setLine
{
    //
    //
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuHeaderViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MenuHeaderViewCell class]) forIndexPath:indexPath];
    NSString *title = self.items[indexPath.row];
    cell.titleLabel.text = title;
    if (indexPath.row == _currentIndex)
    {
        cell.titleLabel.textColor = [UIColor redColor];
        [cell showLine];
    }
    else
    {
        cell.titleLabel.textColor = [UIColor darkGrayColor];
        [cell hideLine];
    }
    return cell;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.currentIndex = indexPath.row;
    
    
    if (_delegate && [_delegate respondsToSelector:@selector(menuView:didSelectItem:)])
    {
        [_delegate menuView:self didSelectItem:indexPath.row];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //NSString *item = self.items[indexPath.row];
    //CGFloat width = [item boundingRectWithSize:CGSizeMake(MAXFLOAT, collectionView.frame.size.height) options:0 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.width + 16;
    CGFloat  width = SCREEN_WIDTH/self.items.count;
    return CGSizeMake(width, collectionView.frame.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 5, 0, 5);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}

@end
